import { Body, Controller, Post } from '@nestjs/common';
import { ApiOkResponse, ApiTags } from '@nestjs/swagger';
import { CrudRequest, GetManyDefaultResponse } from '@dataui/crud';

import { User } from './user.entity';
import { UsersService } from './users.service';
import { creatController, CrudPro } from '@nest-crud/crud-plus';
import { FindManyOptionsReq } from '@nest-crud/request-typeorm/find-options';
import { transOrmFind } from '@nest-crud/request-typeorm/trans-orm-find';

@CrudPro({
  model: {
    type: User,
  },
  params: {
    companyId: {
      field: 'companyId',
      type: 'number',
    },
    id: {
      field: 'id',
      type: 'number',
      primary: true,
    },
  },
  query: {
    softDelete: true,
    join: {
      company: {
        exclude: ['description'],
      },
      'company.projects': {
        alias: 'pr',
        exclude: ['description'],
      },
      profile: {
        eager: true,
        exclude: ['updatedAt'],
      },
    },
  },
})
@ApiTags('users')
@Controller('/companies/:companyId/users')
export class UsersController extends creatController(User, {
  omitMethods: ['createOneBase'],
}) {
  constructor(public service: UsersService) {
    super(service);
    setTimeout(() => {
      console.log(`this['createOneBase']`, this['createOneBase']);
      console.log(`this['getManyBase']`, this['getManyBase']);
    }, 100);
  }
  @ApiOkResponse({ type: User })
  override getManyBase(req: CrudRequest): Promise<User[] | GetManyDefaultResponse<User>> {
    return super.getManyBase(req);
  }
  @Post('find')
  find(@Body() find: FindManyOptionsReq): any {
    const find2 = transOrmFind(find);
    console.log('find2', find2);
    return this.service.find(find2);
  }
}
// export class UsersController implements CrudController<User> {
//   constructor(public service: UsersService) {}

//   get base(): CrudController<User> {
//     return this;
//   }

//   @Override('getManyBase')
//   getAll(@ParsedRequest() req: CrudRequest) {
//     return this.base.getManyBase(req);
//   }
// }
